;(function($){
    $.fn.extend({
        uploadFile:function(option){
            var that = this;
            var defau = {
                type:"post",
                cache:false,
                url:"",
                data:{},
                processData:false,
                contentType:false,
                success:function(){},
                error:function(){},
                progress:function(){},
                sendBefore:function(){},
                filter:[],  //可以接受的文件后缀
                upName:true //是否对文件名称转化大写比对

            }
            option = $.extend(true, defau, option);

            var    fileP = that.attr("name") || "file";  //传给后端得 file对应字段
            console.log(fileP)
            var files = that[0].files;

            option.sendBefore(files);  //发送之前

            for(var i = 0,len = files.length; i < len; i++ ){
                var fs = files[i];
                var fnameArr = fs.name.split('.');
                var fNmae = fnameArr.pop();
                var str = '';
                if(option.upName){
                    fNmae = fNmae.toUpperCase();
                }else{
                    fNmae = fNmae.toLowerCase();
                }
                if(option.filter.length > 0 && option.filter.indexOf(fNmae) !== -1){
                    option.error("文件后缀不符",i);
                    continue;
                }
                fileUpload(files[i],i);

            }

            //开始文件上传
            function fileUpload(file,index){
                var fd = new FormData();
                fd.append(fileP,file);

                //追加其他参数
                for(var i in option.data){
                    fd.append(i,option.data[i]);
                }

                $.ajax({
                    url: option.url,
                    type: option.type,
                    data: fd,
                    cache: option.cache,
                    processData: option.processData,
                    contentType: option.contentType,
                    success:function(data){
                        option.success(data,index);

                    },
                    error:function(error){
                        console.log(error);
                        option.error(error,index);
                    },
                    xhr: function(){
                        var xhr = $.ajaxSettings.xhr();
                        if(onprogress && xhr.upload) {
                            xhr.upload.addEventListener("progress" , onprogress, false);
                            return xhr;
                        }
                    }
                });

                function onprogress(evt){
                    var loaded = evt.loaded;     //已经上传大小情况
                    var tot = evt.total;      //附件总大小
                    var per = Math.floor(100*loaded/tot);  //已经上传的百分比
                    file.loaded = loaded;
                    file.total = tot;
                    file.percent = per + '%';
                    file.index = index;
                    option.progress(file);
                }
            }
            return that;
        }
    });

})($,window);